package com.gx.smart.building.admin;

import com.casic.gx.grpc.common.ComQuery;
import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.smart.lib.http.api.asynctask.GrpcAsyncTask;
import com.gx.smart.lib.http.api.config.ApiConfig;
import com.gx.smart.lib_http.BuildConfig;
import com.huishi.gx.ib.grpc.lib.ib.admin.workorder.v1.QueryWorkOrderEventOpinionSettingListReq;
import com.huishi.gx.ib.grpc.lib.ib.admin.workorder.v1.QueryWorkOrderEventOpinionSettingListResp;
import com.huishi.gx.ib.grpc.lib.ib.admin.workorder.v1.WorkOrderEventOpinionSettingApiGrpc;
import com.huishi.gx.ib.grpc.lib.ib.admin.workorder.v1.WorkOrderEventOpinionSettingDto;
import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;

/**
 * @author: xiaosy
 * 时间: 2022/6/16
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public class WorkOrderEventOpinionSettingApiService {
    private String host = BuildConfig.JOYLIFE_GRPC_UAA_SERIVER_URL;
    private String port = "80";

    private WorkOrderEventOpinionSettingApiService() {

    }

    public static WorkOrderEventOpinionSettingApiService getInstance() {
        return SingleTonHolder.INSTANCE;
    }

    /**
     * 获取AuthStub  设置超时时间  8秒
     *
     * @param channel
     * @return
     */
    private WorkOrderEventOpinionSettingApiGrpc.WorkOrderEventOpinionSettingApiBlockingStub getStub(ManagedChannel channel) {
        return WorkOrderEventOpinionSettingApiGrpc.newBlockingStub(channel)
                .withDeadlineAfter(ApiConfig.TIMEOUT_NETWORK, TimeUnit.SECONDS);

    }

    /**
     * 查询工单快捷回复列表
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, QueryWorkOrderEventOpinionSettingListResp>
    queryWorkOrderEventOpinionSettingList(
            String eventCode,
            CallBack<QueryWorkOrderEventOpinionSettingListResp> callBack) {
        return new GrpcAsyncTask<String, Void, QueryWorkOrderEventOpinionSettingListResp>(callBack) {
            @Override
            protected QueryWorkOrderEventOpinionSettingListResp doRequestData(ManagedChannel channel) {
                QueryWorkOrderEventOpinionSettingListReq message = QueryWorkOrderEventOpinionSettingListReq.newBuilder()
                        .setComQuery(ComQuery.newBuilder().setPage(1).setPageCount(10000).build())
                        .setWorkOrderEventOpinionSettingDto(WorkOrderEventOpinionSettingDto.newBuilder().setEventCode(eventCode).build())
                        .build();
                Logger.d(message);
                QueryWorkOrderEventOpinionSettingListResp response = null;
                try {
                    response = getStub(channel).queryWorkOrderEventOpinionSettingList(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "queryWorkOrderEventOpinionSettingList");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    private static class SingleTonHolder {
        private static final WorkOrderEventOpinionSettingApiService INSTANCE = new WorkOrderEventOpinionSettingApiService();
    }
}
